package com.mes.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.mes.cloud.dao.BmeBcDAO;
import com.mes.cloud.dao.BmeBcFiledDAO;
import com.mes.cloud.dao.BmeBcPickmapDAO;
import com.mes.cloud.dto.BcViewDTO;
import com.mes.cloud.dto.BmeBcDTO;
import com.mes.cloud.dto.BmeBcFiledDTO;
import com.mes.cloud.dto.BmeBcPickmapDTO;
import com.mes.cloud.service.BmeBcFiledService;
import com.mes.cloud.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BC_Filed基础表
 *
 * @author xhy
 * @version 1.0
 * @date 2019/05/09 16:00
 */
@Service
public class BmeBcFiledServiceImpl implements BmeBcFiledService {
    /**
     * 日志记录  其中在记录的时候 Class代表类名 Method代表方法名 Param代表参数
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BmeBcFiledServiceImpl.class);

    @Autowired
    private BmeBcFiledDAO bmeBcFiledDAO;
    @Autowired
    private BmeBcPickmapDAO bmeBcPickmapDAO;
    @Autowired
    private BmeBcDAO bmeBcDAO;

    /**
     * 根据条件获取BC_Filed基础表列表
     *
     * @param condition
     * @return List<BmeBcFiledDTO>
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public List<BmeBcFiledDTO> getBmeBcFiledList(Map<Object, Object> condition) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledList: InParam condition: {}", condition.toString());
        List<BmeBcFiledDTO> bmeBcFiledDTOS = bmeBcFiledDAO.getBmeBcFiledList(condition);
        for (BmeBcFiledDTO bmeBcFiledDTO : bmeBcFiledDTOS) {
            if (!Strings.isNullOrEmpty(bmeBcFiledDTO.getFieldPickbc())) {
                BmeBcPickmapDTO bmeBcPickmapById = bmeBcPickmapDAO.getBmeBcPickmapById(bmeBcFiledDTO.getFieldPickbc());
                bmeBcFiledDTO.setPickname(bmeBcPickmapById.getName());
            }
        }
        if (bmeBcFiledDTOS == null) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledList: OutParam bmeBcFiledDTOS==null");
            return null;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledList: OutParam bmeBcFiledDTOS: {}", bmeBcFiledDTOS.toString());
        return bmeBcFiledDTOS;
    }

    /**
     * 获取BC_Filed基础表的最大记录数
     *
     * @param condition
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long getBmeBcFiledCount(Map<Object, Object> condition) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledCount: InParam condition: {}", condition.toString());
        long count = bmeBcFiledDAO.getBmeBcFiledCount(condition);
        if (count == 0) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledCount: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledCount: OutParam count: {}", count);
        return count;
    }

    /**
     * 新增BC_Filed基础表
     *
     * @param bmeBcFiledDTO
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long insertBmeBcFiled(BmeBcFiledDTO bmeBcFiledDTO) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method insertBmeBcFiled: InParam bmeBcFiledDTO: {}", bmeBcFiledDTO.toString());
        bmeBcFiledDTO.setId(UuId.getShortUuid());
        long count = bmeBcFiledDAO.insertBmeBcFiled(bmeBcFiledDTO);
        if (count == 0) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method insertBmeBcFiled: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method insertBmeBcFiled: OutParam count: {}", count);
        return count;
    }

    /**
     * 更新BC_Filed基础表
     *
     * @param bmeBcFiledDTO
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long updateBmeBcFiled(BmeBcFiledDTO bmeBcFiledDTO) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method updateBmeBcFiled: InParam bmeBcFiledDTO: {}", bmeBcFiledDTO.toString());
        long count = bmeBcFiledDAO.updateBmeBcFiled(bmeBcFiledDTO);
        if (count == 0) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method updateBmeBcFiled: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method updateBmeBcFiled: OutParam count: {}", count);
        return count;

    }

    /**
     * 删除BC_Filed基础表
     *
     * @param id
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long delBmeBcFiledById(String id) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method delBmeBcFiledById: InParam id: {}", id);
        long count = bmeBcFiledDAO.delBmeBcFiledById(id);
        if (count == 0) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method delBmeBcFiledById: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method delBmeBcFiledById: OutParam count: {}", count);
        return count;
    }

    /**
     * 根据BC_Filed基础表标识获取BC_Filed基础表
     *
     * @param id
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public BmeBcFiledDTO getBmeBcFiledById(String id) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledById: InParam id: {}", id);
        BmeBcFiledDTO bmeBcFiledDTO = bmeBcFiledDAO.getBmeBcFiledById(id);
        if (bmeBcFiledDTO == null) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledById: OutParam bmeBcFiledDTO==null");
            return null;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledById: OutParam bmeBcFiledDTO: {}", bmeBcFiledDTO.toString());
        return bmeBcFiledDTO;
    }

    /**
     * 根据条件获取BC_Filed基础表列表并进行分页
     *
     * @param condition
     * @return List<BmeBcFiledDTO>
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public CommPager<BmeBcFiledDTO> getBmeBcFiledListPage(Map<Object, Object> condition) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledListPage: InParam condition: {}", condition.toString());
        int pageIndex = condition.get("pageIndex") == null ? 0 : Integer.valueOf(condition.get("pageIndex").toString());
        int pageSize = condition.get("pageSize") == null ? 10 : Integer.valueOf(condition.get("pageSize").toString());
        Page<BmeBcFiledDTO> page = PageHelper.startPage(pageIndex, pageSize, true);
        List<BmeBcFiledDTO> bmeBcFiledList = bmeBcFiledDAO.getBmeBcFiledList(condition);
        if (bmeBcFiledList == null) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledListPage: OutParam bmeBcFiledList==null");
            return null;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method getBmeBcFiledListPage: OutParam bmeBcFiledDTO: {}", bmeBcFiledList.toString());
        CommPager<BmeBcFiledDTO> pageResult = new CommPager<>(page, bmeBcFiledList);
        return pageResult;
    }

    /**
     * 对BC_Filed基础表标进行批量删除操作
     *
     * @param map
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    @Transactional
    public long batchDelete(Map<Object, Object> map) {
        LOGGER.info("Class BmeBcFiledServiceImpl: Method batchDelete: InParam map: {}", map);
        List<Long> longs = (List<Long>) map.get("ids");
        long count = bmeBcFiledDAO.batchDelete(longs);
        if (count == 0) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method batchDelete: OutParam count==0");
            return 0;
        }
        LOGGER.info("Class BmeBcFiledServiceImpl: Method batchDelete: OutParam count: {}", count);
        return count;
    }

    /**
     * 对BC_Filed是否启用
     *
     * @param map
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long enable(Map<Object, Object> map) {
        if (map.get("id") == null || map.get("isEnable") == null) {
            LOGGER.info("Class BmeBcFiledServiceImpl: Method enable: map ==null");
            return 0;
        }
        return bmeBcFiledDAO.enable(map);
    }

    @Override
    public int importExcel(MultipartFile file) throws Exception {
        String rooPath = this.getClass().getResource("/").getPath();
        //复制文件到static目录
        String path = rooPath + "/static";
        String fileName = FileUploadUtils.uploadFile(file, path);
        String filePath = FileUploadUtils.uploadFile(file, path, fileName);
        FileInputStream fis = null;
        fis = new FileInputStream(filePath);
        // 创建工具类.
        ExcelUtil<BmeBcFiledDTO> util = new ExcelUtil<BmeBcFiledDTO>(BmeBcFiledDTO.class);
        List<BmeBcFiledDTO> paramList = util.importExcel(fileName, fis);
        HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
        if (StringUtils.isEmpty(paramList)) {
            throw new Exception("数据为空！");
        }
        for (BmeBcFiledDTO bmeBcFiledDTO : paramList) {
            if (StringUtils.isEmpty(bmeBcFiledDTO.getId())) {
                bmeBcFiledDTO.setId(UuId.getShortUuid());
            } else {
                objectObjectHashMap.put("id", bmeBcFiledDTO.getId());
                long count = bmeBcFiledDAO.getBmeBcFiledCount(objectObjectHashMap);
                if (count > 0) {
                    throw new Exception("数据已经存在！");
                }
            }
        }
        return bmeBcFiledDAO.insertExcel(paramList);
    }

    @Override
    public void exportFile(Map<Object, Object> map, HttpServletResponse response) throws Exception {
        List<BmeBcFiledDTO> list = bmeBcFiledDAO.getBmeBcFiledList(map);
        if (list.size() == 0) {
            throw new Exception("导出数据为空！");
        }
        // 创建工具类
        ExcelUtil<BmeBcFiledDTO> util = new ExcelUtil<BmeBcFiledDTO>(BmeBcFiledDTO.class);
        // 导出
        String topName = DateUtil.getDateTime("yyyyMMddHHmmss");
        util.exportFile(list, DateUtil.getDateTime(topName), topName, response);
    }

    @Override
    public List<BmeBcFiledDTO> findListByCodePickmapCode(Map<Object, Object> map) {
        if(!map.containsKey("bccode") || !map.containsKey("pickmapcode")){
            throw new RuntimeException("bccode-pickmapcode参数异常！");
        }
        //BC编码
        String bccode = map.get("bccode").toString();
        //picmap编码
        String pickmapcode = map.get("pickmapcode").toString();
        //获取BC信息
        BmeBcDTO bmeBcDTO = bmeBcDAO.getBmeBcByCode(bccode);
        if(bmeBcDTO == null){
            throw new RuntimeException("bccode找不到bc信息！");
        }
        //获取picmap信息
        Map mapPickmapParam = new HashMap();
        mapPickmapParam.put("pid",bmeBcDTO.getId());
        mapPickmapParam.put("code",map.get("picmapCode"));
        List<BmeBcPickmapDTO>  bmeBcPickmapDTOList = bmeBcPickmapDAO.getBmeBcPickmapList(mapPickmapParam);
        if(CollectionUtils.isEmpty(bmeBcPickmapDTOList)){
            throw new RuntimeException("bmeBcPickmapDTOList为空");
        }
        BmeBcPickmapDTO bmeBcPickmapDTOTemp = bmeBcPickmapDTOList.get(0);
        //获取BC信息
        BmeBcDTO bmeBcPickmap = bmeBcDAO.getBmeBcByCode(bmeBcPickmapDTOTemp.getPickbc());

        //获取picmap信息
        Map mapBmebcParam = new HashMap();
        mapBmebcParam.put("pid",bmeBcPickmap.getId());
        List<BmeBcFiledDTO> list = bmeBcFiledDAO.getBmeBcFiledList(mapBmebcParam);
        return list;
    }
}
